package com.cnswhy.cloud.server.app.controller;

import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.core.constant.RedisConstant;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.app.exception.LoginExecption;
import com.cnswhy.cloud.server.core.vo.RespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制类基类，所以的控制类继承这个类
 *
 * @author HuangHL
 * @date 2015/12/4
 * @see
 */
@Controller
@RequestMapping("/")
@Slf4j
public class BaseController {


    @Autowired
    private RedisService redisService;

    public String ipAddr;

    public Integer port;

    public String userName;

    public String pwd;

    public String dir;

    /**
     * 描述：获取FTP信息
     */
    public void getFtpInfo() {
        ipAddr = String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FILE_FTP_IP));

        port = Integer
                .valueOf(String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FILE_FTP_PORT)));

        userName = String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FILE_FTP_USER));

        pwd = String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FILE_FTP_PWD));

        dir = String.valueOf(redisService.hget(RedisConstant.SYS_CONFIG_1, RedisConstant.FTP_PATH_IMAGE));
    }


    public String getLoginIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (!checkIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (!checkIP(ip)) {
            ip = request.getRemoteAddr();
        }

        if (null != ip && ip.indexOf(',') != -1) {
            //如果通过了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串 IP 值
            //取X-Forwarded-For中第一个非unknown的有效IP字符串
            //如：X-Forwarded-For：192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100
            //用户真实IP为： 192.168.1.110 
            String[] ips = ip.split(",");
            for (int i = 0; i < ips.length; i++) {
                if (null != ips[i] && !"unknown".equalsIgnoreCase(ips[i])) {
                    ip = ips[i];
                    break;
                }
            }
        }

        return ip;
    }

    private boolean checkIP(String ip) {
        if (ip == null || ip.length() == 0 || "unkown".equalsIgnoreCase(ip)) {
            return false;
        }
        return true;
    }


    public Customer getLoginCustomer(HttpServletRequest request) throws LoginExecption {
        String sessionId = (String) request.getHeader("sessionId");
        Object obj = redisService.hget(RedisConstant.CUSTOMER_SESSION_KEY, sessionId);
        if (null == obj) {
            throw new LoginExecption(RespVo.LOGIN_TIMEOUT, "已超时,请重新登录");
        }
        Customer customer = (Customer) obj;
        redisService.hset(RedisConstant.CUSTOMER_SESSION_KEY, sessionId, customer, 60 * 60 * 24L);
        return customer;
    }

    /**
     * 更新redis中的用户信息
     *
     * @param redisService
     * @param request
     * @param customer
     * @return
     * @throws LoginExecption
     */
    public Customer updateLoginCustomer(RedisService redisService, HttpServletRequest request, Customer customer) throws LoginExecption {
        String sessionId = (String) request.getHeader("sessionId");
        redisService.hset(RedisConstant.CUSTOMER_SESSION_KEY, sessionId, customer, 60 * 60 * 24L);
        return customer;
    }

}
